/*****************************************************************************
 * tr240-3000.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_INFO
#define LOG_MODULE    "dev.power"

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define POWER_TR240_IDX_TURN 0
#define POWER_TR240_IDX_VOL  1
#define POWER_TR240_IDX_CUR  2
#define POWER_TR240_IDX_TMP  3

#define POWER_TR240_ADDR_TURN 0x0005 /** 开关 **/
#define POWER_TR240_ADDR_VOL  0x0000 /** 输出电压 **/
#define POWER_TR240_ADDR_CUR  0x0001 /** 输出电流 **/
#define POWER_TR240_ADDR_TMP  0x000A /** 温度 **/
#define POWER_TR240_ADDR_
/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef enum {
    POWER_TR240_TURN_OFF,
    POWER_TR240_TURN_ON,
} power_tr240_turn_t;

typedef struct {
    plum_u16 addr;
    plum_u16 data;
} power_tr240_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE
power_tr240_t power_tr240[] = {
    [POWER_TR240_IDX_TURN] = {POWER_TR240_ADDR_TURN, POWER_TR240_TURN_OFF},
    [POWER_TR240_IDX_VOL]  = {POWER_TR240_ADDR_VOL, 0},
    [POWER_TR240_IDX_CUR]  = {POWER_TR240_ADDR_CUR, 0},
    [POWER_TR240_IDX_TMP]  = {POWER_TR240_ADDR_TMP, 0},
};

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void power_tr240_notice(modbus_protocol_evt evt, plum_void *arg)
{
    switch (evt) {
        case MODBUS_EVT_MSG:
        {
            modbus_msg_t *msg = (modbus_msg_t *)arg;
            plum_u32      loop;
            for (loop = 0; loop < PLUM_ARRAY_CNT(power_tr240); loop++) {
                if (power_tr240[loop].addr == msg->addr) {
                    LOG_D("notice msg : 0x%04X", *(plum_u16 *)msg->value);
                    power_tr240[loop].data = *(plum_u16 *)msg->value;
                    break;
                }
            }
        } break;

        case MODBUS_EVT_ERR:
        {
            modbus_err_t *msg = (modbus_err_t *)arg;
            LOG_E("modbus notice error,rc : %d", msg->errcode);
            LOG_E("reason : %s", msg->discr);
        } break;

        default:
            break;
    }
}

PLUM_PRIVATE
PLUM_INLINE plum_s32 power_tr240_write_turn(plum_u32           id,
                                            power_tr240_turn_t turn)
{
    plum_u16 data = turn;
    return modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                 MODBUS_PROTOCOL_WRITE_SHOLD,
                                 POWER_TR240_ADDR_TURN, &data, 2);
}

PLUM_PRIVATE
plum_s32 power_tr240_read_turn(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(power_tr240_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc : %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  POWER_TR240_ADDR_TURN, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc : %d", rc);
            break;
        }

        rc = power_tr240[POWER_TR240_IDX_TMP].data;
        LOG_D("turn : %d", rc);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
PLUM_INLINE plum_s32 power_tr240_write_voltage(plum_u32 id, plum_u16 voltage)
{
    plum_u16 data = voltage;
    return modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                 MODBUS_PROTOCOL_WRITE_SHOLD,
                                 POWER_TR240_ADDR_VOL, &data, 2);
}

PLUM_PRIVATE
plum_s32 power_tr240_read_voltage(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(power_tr240_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc : %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  POWER_TR240_ADDR_VOL, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc : %d", rc);
            break;
        }

        LOG_D("valtage : %d", power_tr240[POWER_TR240_IDX_VOL].data);
        rc = power_tr240[POWER_TR240_IDX_VOL].data;
    } while (0);

    return (rc);
}

PLUM_PRIVATE
PLUM_INLINE plum_s32 power_tr240_write_current(plum_u32 id, plum_u16 current)
{
    plum_u16 data = current;
    return modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                 MODBUS_PROTOCOL_WRITE_SHOLD,
                                 POWER_TR240_ADDR_CUR, &data, 2);
}

PLUM_PRIVATE
plum_s32 power_tr240_read_current(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(power_tr240_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc : %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  POWER_TR240_ADDR_CUR, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc : %d", rc);
            break;
        }

        rc = power_tr240[POWER_TR240_IDX_CUR].data;
    } while (0);

    return (rc);
}

PLUM_PRIVATE
PLUM_INLINE plum_s32 power_tr240_write_temperature(plum_u32 id, plum_u16 tmp)
{
    plum_u16 data = tmp;
    return modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                 MODBUS_PROTOCOL_WRITE_SHOLD,
                                 POWER_TR240_ADDR_TMP, &data, 2);
}

PLUM_PRIVATE
plum_s32 power_tr240_read_temperature(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(power_tr240_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc : %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  POWER_TR240_ADDR_TMP, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc : %d", rc);
            break;
        }

        rc = power_tr240[POWER_TR240_IDX_TMP].data;
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 power_tr240_init(plum_device_ref_t dev)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        PLUM_UNUSED(dev);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 power_tr240_read(plum_device_ref_t dev, plum_dev_read_t *arg)
{
    PLUM_UNUSED(dev);

    plum_s32 rc = PLUM_ECODE_OK;

    do {
        LOG_D("offset : %d", arg->offset);
        switch (arg->offset) {
            case 1:
                rc = power_tr240_read_turn(1);
                if (rc < 0) {
                    LOG_E("power_tr240_read_turn err,rc : %d", rc);
                    rc = PLUM_ECODE_EIO;
                    break;
                }
                memcpy(arg->rData, &rc, 4);
                rc = PLUM_ECODE_OK;
                break;

            case 2:
                LOG_D("power_tr240_read_voltage");
                rc = power_tr240_read_voltage(1);
                if (rc < 0) {
                    LOG_E("power_tr240_read_voltage err,rc : %d", rc);
                    rc = PLUM_ECODE_EIO;
                    break;
                }
                LOG_D("rc : %d", rc);
                memcpy(arg->rData, &rc, 4);
                rc = PLUM_ECODE_OK;
                break;

            case 3:
                LOG_D("power_tr240_read_current");
                rc = power_tr240_read_current(1);
                if (rc < 0) {
                    LOG_E("power_tr240_read_current err,rc : %d", rc);
                    rc = PLUM_ECODE_EIO;
                    break;
                }
                LOG_D("rc : %d", rc);
                memcpy(arg->rData, &rc, 4);
                rc = PLUM_ECODE_OK;
                break;

            case 4:
                rc = power_tr240_read_temperature(1);
                if (rc < 0) {
                    LOG_E("power_tr240_read_temperature err,rc : %d", rc);
                    rc = PLUM_ECODE_EIO;
                    break;
                }
                memcpy(arg->rData, &rc, 4);
                rc = PLUM_ECODE_OK;
                break;

            default:
                LOG_E("not support the id");
                break;
        }
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 power_tr240_write(plum_device_ref_t dev, plum_dev_write_t *arg)
{
    PLUM_UNUSED(dev);

    plum_s32 rc = PLUM_ECODE_OK;

    do {
        switch (arg->offset) {
            case 1:
                rc = power_tr240_write_turn(arg->wData[0],
                                            *(plum_u16 *)(arg->wData));
                if (rc < 0) {
                    LOG_E("power_tr240_write_turn err,rc : %d", rc);
                }
                break;

            case 2:
                rc = power_tr240_write_voltage(arg->wData[0],
                                               *(plum_u16 *)(arg->wData));
                if (rc < 0) {
                    LOG_E("power_tr240_read_voltage err,rc : %d", rc);
                }
                break;

            case 3:
                rc = power_tr240_write_current(arg->wData[0],
                                               *(plum_u16 *)(arg->wData));
                if (rc < 0) {
                    LOG_E("power_tr240_read_current err,rc : %d", rc);
                }
                break;

            default:
                LOG_E("not support the id");
                break;
        }
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 power_tr240_iotctrl(plum_device_ref_t dev, plum_dev_ioctrl_t *arg)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        PLUM_UNUSED(dev);
        PLUM_UNUSED(arg);
    } while (0);

    return (rc);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_dev_api_t tr240_api = {
    .init   = power_tr240_init,
    .read   = power_tr240_read,
    .write  = power_tr240_write,
    .ioctrl = power_tr240_iotctrl,
};
PLUM_DEVICE_EXPORT(tr240, &tr240_api);

/*****************************************************************************
 * Cli Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void tr240_cli_list_reg(plum_u32 argc, plum_s8 **argv)
{
    PLUM_UNUSED(argc);
    PLUM_UNUSED(argv);

    plum_u32 loop;
    for (loop = 0; loop < PLUM_ARRAY_CNT(power_tr240); loop++) {
        LOG_I("power tr240 parameter[%d] : 0x%04X", loop,
              power_tr240[loop].data);
    }
}
PLUM_CAL_CLI_EXPORT("tr240_list", "list all tr240 parameter",
                    tr240_cli_list_reg);
/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
